package org.makcro.sms.services.chat.base.filters;

import java.util.Date;

import org.makcro.sms.services.chat.base.dao.AnswerDao;
import org.makcro.sms.services.chat.base.dao.QuestionDao;
import org.makcro.sms.services.chat.model.Answer;
import org.makcro.sms.services.chat.model.Question;

import com.klikmee.sms.cef.CoreAdapter;
import com.klikmee.sms.core.commons.filters.ProcessItemFilter;

public abstract class AbstractFilter implements ProcessItemFilter<Question> {

	/**
	 * Instance of core adapter used to send
	 */
	protected CoreAdapter	coreAdapter;
	/**
	 * Ref to the answer dao
	 */
	protected AnswerDao		answerDAO				= null;
	/**
	 * Keyword that activates this listener. Listener matches message text in
	 * search for keyword
	 * 
	 */
	protected QuestionDao	questionDAO				= null;
	/**
	 * Regular expression which if matched activates this listener.
	 */
	protected String		activationExpression	= null;

	public AbstractFilter() {
		super();
	}

	/**
	 * Persists {@link Answer} object into database
	 * 
	 * @param question
	 * @param messageText
	 * @param refCoreId
	 * @param visible
	 *            TODO
	 */
	protected void persistAnswer(Question question, String messageText,
			Long refCoreId, boolean visible) {
		Answer answer = new Answer();
		answer.setAnswer( messageText );
		answer.setCoreID( refCoreId );
		answer.setDateAnswered( new Date() );
		answer.setFromNumber( question.getToNumber() );
		answer.setQuestion( question );
		answer.setToNumber( question.getFromNumber() );
		answer.setVisable( visible );
		// cascade persist question
		this.answerDAO.create( answer );
	}

	public void setActivationExpression(String activationExpression) {
		this.activationExpression = activationExpression;
	}

	public void setAnswerDAO(AnswerDao answerDAO) {
		this.answerDAO = answerDAO;
	}

	public void setQuestionDAO(QuestionDao questionDAO) {
		this.questionDAO = questionDAO;
	}

	public void setCoreAdapter(CoreAdapter coreAdapter) {
		this.coreAdapter = coreAdapter;
	}

}